import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class StringLocationObject {

//TODO: I am sure this object doesn't have all the functions it needs yet, if you see ones it should have please add them.
    
    //have a hash map with the key being the filename and the value a list of integers
    HashMap<String,ArrayList<Integer>> locations;

    public StringLocationObject() {
        this.locations = new HashMap<String,ArrayList<Integer>>();
    }

//This will only work for something with only 1 filename, you must add the others manually
    public StringLocationObject(String f, ArrayList<Integer> i){
        this.locations = new HashMap<String,ArrayList<Integer>>();
        locations.put(f,i);
    }

    public void addLocation(String filename, Integer newLocation){
        //if it does not contain any with this filename, simply put it in
    	if (!locations.containsKey(filename)) locations.put(filename, new ArrayList<Integer>());
        //otherwise just add to the current arraylist
    	locations.get(filename).add(newLocation);
    }

    public void addLocations(String filename, ArrayList<Integer> newLocations){
        //if it does not contain any with this filename, simply put it in
    	if (!locations.containsKey(filename)) locations.put(filename, new ArrayList<Integer>());
        //otherwise just add to the current arraylist
    	locations.get(filename).addAll(newLocations);
    }
    
    //added for convince sake
    public void addLocations(StringLocationObject l){
        HashMap<String,ArrayList<Integer>> k = l.getLocationMap();
        //iterate for each filename
        for(String str: k.keySet()){
            if(locations.containsKey(str)){
                locations.get(str).addAll(k.get(str));
            } else {
                locations.put(str,k.get(str));
            }
        }
    }

	public Set<String> getFilenames() {
		return locations.keySet();
	}

    public ArrayList<Integer> getLocations(String f){
        return locations.get(f);
    }

	public HashMap<String,ArrayList<Integer>> getLocationMap() {
		return this.locations;
	}

    //return the raw number of times this string was found
    public Integer size(){
        Integer counter = 0;
        //iterate for each filename
        for(ArrayList<Integer> list: locations.values()){
            counter += list.size();
        }
        return counter;
    }
    
    public String toString() {
    	String out = "";
    	for (Map.Entry<String, ArrayList<Integer>> entry : locations.entrySet()) {
    		out += "<'" + entry.getKey() + "'";
    		for (Integer i : entry.getValue()) {
    			out += ", " + i;
    		}
    		out += ">";
    	}
    	return out;
    }
}
